package providev.learn.imagina.tema2_3_s;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Principal extends Activity {
    private static final int SHOW_SUBACTIVITY = 1;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.principal);
        
        Button boton = (Button) findViewById(R.id.button1);
        boton.setOnClickListener(new OnClickListener() {
                                
          public void onClick(View v) {
        	  	String action = "providev.learn.imagina.tema2.SECUNDARIA";
                  Intent intent = new Intent(action);
                  startActivityForResult(intent,SHOW_SUBACTIVITY);

          }
        });

    }
    
    @Override 
	public void onActivityResult(int requestCode, int resultCode, Intent data) {     
	  super.onActivityResult(requestCode, resultCode, data); 
	  switch(requestCode) { 
	    case (SHOW_SUBACTIVITY) : { 
	      if (resultCode == Activity.RESULT_OK) { 
	    	  String valorDevuelto = data.getStringExtra("valorAleatorio");
	    	  AlertDialog.Builder ad = new AlertDialog.Builder(Principal.this);
	    	  ad.setTitle("Resultado random devuelto");
	    	  ad.setMessage(valorDevuelto);
	    	  ad.setCancelable(true);
	    	  ad.setNegativeButton("No", new DialogInterface.OnClickListener() {
	              public void onClick(DialogInterface dialog, int id) {
	                  dialog.cancel();
	             }
	         });

	    	  ad.show();


          }
        break; 
	    } 
	   } 
         }

}